
using System;
using System.Text ;
using System.Data ;
using System.Collections ;

using WP.MF.BusinessRule; 
using WP.MF.DataEntity; 
using WP.MF.DataAccess; 
using WP.Commons;
using WP.DB;

namespace WP.MF.BusinessLogical
{
	/// <summary>
	/// Summary description for MBranchBLL.
	/// </summary>
    public class MBranchBLL : IBusinessLogical
	{
		MBranchDAL mBranchDAL = new MBranchDAL();
		public MBranchBLL()
		{
		}

		#region Get All MBranch main info
		public MBranchDEL SelectMBranchByKey(string strRecID)
		{
			DBAccess dBAccess=new DBAccess(false,false);

			try
			{
				//Get MBranch Info
				MBranchDEL mBranchDEL=mBranchDAL.SelectMBranchByKey(dBAccess,strRecID);

				return mBranchDEL;
			}
			catch(Exception e)
			{
				ExceptionManager.ProcessException(e);
				return null;
			}
			finally
			{
				dBAccess.CloseCon();
			}
		}
		#endregion

        public DataView SelectBranchForList()
        {
            DBAccess dBAccess = new DBAccess();

            return mBranchDAL.SelectBranchForList(dBAccess);
        }

        #region ***For implement interface

        #region Select Record by RECID OverLoad +1
        public MBranchDEL SelectDataByRecID(DBAccess dBAccess, string strRecId)
        {
            MBranchDEL mBranchDEL = new MBranchDEL();

            DataFunctions.SelectByRecID(dBAccess, mBranchDEL, mBranchDAL.Fields, strRecId);

            return mBranchDEL;
        }

        public IDataEntity SelectDataByRecID(string strRecId)
        {
            DBAccess dBAccess = new DBAccess(false, false);
            try
            {
                MBranchDEL mBranchDEL = SelectDataByRecID(dBAccess, strRecId);
                return mBranchDEL as IDataEntity;
            }
            catch (Exception e)
            {
                ExceptionManager.ProcessException(e);
                return null;
            }
            finally
            {
                dBAccess.CloseCon();
            }
        }
        #endregion

        #region Insert into MBranch
        public bool Insert(DBAccess dBAccess, MBranchDEL mBranchDEL)
        {
            bool bFlag = true;
            MBranchBRL mBranchBRL = new MBranchBRL();

            //BaseFunctions.SpecialCharDeal(mBranchDEL);

            if (mBranchBRL.InsertValid(dBAccess, mBranchDEL))
            {
                mBranchDAL.InsertMBranch(dBAccess, mBranchDEL);
            }
            else
            {
                bFlag = false;
            }
            return bFlag;

        }
        public bool Insert(IDataEntity iMBranchDEL)
        {
            bool bReturn = true;
            DBAccess dBAccess = null;
            MBranchDEL mBranchDEL = iMBranchDEL as MBranchDEL;
            dBAccess = new DBAccess(false, true);

            try
            {
                bReturn = Insert(dBAccess, mBranchDEL);
                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTran();
                bReturn = false;
                ExceptionManager.ProcessException(e);
            }

            return bReturn;
        }
        #endregion

		#region Update MBranch
        public bool Update(DBAccess dBAccess, MBranchDEL mBranchDEL)
        {
            bool bFlag = true;
            MBranchBRL mBranchBRL = new MBranchBRL();

            //BaseFunctions.SpecialCharDeal(mBranchDEL);

            if (mBranchBRL.UpdateValid(dBAccess, mBranchDEL))
            {
                mBranchDAL.UpdateMBranch(dBAccess, mBranchDEL);
            }
            else
            {
                bFlag = false;
            }
            return bFlag;
        }

        public bool Update(IDataEntity iMBranchDEL)
        {
            bool bReturn = true;
            MBranchDEL mBranchDEL = iMBranchDEL as MBranchDEL;
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                bReturn = Update(dBAccess, mBranchDEL);
                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTran();
                ExceptionManager.ProcessException(e);
                bReturn = false;
            }

            return bReturn;
        }

		#endregion

		#region Delete MBranch
        public bool Delete(string strRecId, byte[] btOldRecStamp)
        {
            MBranchBRL mBranchBRL = new MBranchBRL();
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                if (mBranchBRL.DeleteValid(dBAccess, strRecId, btOldRecStamp))
                {
                    mBranchDAL.DeleteMBranch(dBAccess, strRecId);
                    dBAccess.CommitTranAndCloseCon();

                    return true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                    return false;
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }

		#endregion

        #endregion For implement interface***

        ////public bool InsertMBranch(IDataEntity iMBranchDEL)
        ////{
        ////    MBranchDEL mBranchDEL = iMBranchDEL as MBranchDEL;
        ////    DBAccess dBAccess=new DBAccess(false,true);
        ////    MBranchBRL mBranchBRL=new MBranchBRL();

        ////    try
        ////    {
        ////        //BaseFunctions.SpecialCharDeal(mBranchDEL);

        ////        if(mBranchBRL.InsertValid(dBAccess,mBranchDEL))
        ////        {
        ////            //Insert Into MBranch
        ////            mBranchDAL.InsertMBranch(dBAccess,mBranchDEL);

        ////            dBAccess.CommitTranAndCloseCon();			
        ////            return true;
        ////        }
        ////        else
        ////        {
        ////            dBAccess.RollbackTranAndClose();
        ////            if (SysVar.ErrorMessage.Count > 0)
        ////            {
        ////                MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
        ////            }
        ////            return false;
        ////        }
        ////    }
        ////    catch(Exception e)
        ////    {
        ////        dBAccess.RollbackTranAndClose();
        ////        ExceptionManager.ProcessException(e);
        ////        return false;
        ////    }
        ////}

        ////public bool UpdateMBranch(MBranchDEL mBranchDEL)
        ////{
        ////    DBAccess dBAccess = new DBAccess(false, true);
        ////    MBranchBRL mBranchBRL = new MBranchBRL();

        ////    try
        ////    {
        ////        //BaseFunctions.SpecialCharDeal(mBranchDEL);

        ////        if (mBranchBRL.UpdateValid(dBAccess, mBranchDEL))
        ////        {
        ////            //Update MBranch
        ////            mBranchDAL.UpdateMBranch(dBAccess, mBranchDEL);

        ////            dBAccess.CommitTranAndCloseCon();
        ////            return true;
        ////        }
        ////        else
        ////        {
        ////            dBAccess.RollbackTranAndClose();
        ////            if (SysVar.ErrorMessage.Count > 0)
        ////            {
        ////                MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
        ////            }
        ////            return false;
        ////        }
        ////    }
        ////    catch (Exception e)
        ////    {
        ////        dBAccess.RollbackTranAndClose();
        ////        ExceptionManager.ProcessException(e);
        ////        return false;
        ////    }
        ////}

        ////public bool DeleteMBranch(string strRecId, byte[] btOldRecStamp)
        ////{
        ////    MBranchBRL mBranchBRL = new MBranchBRL();
        ////    DBAccess dBAccess = new DBAccess(false, true);

        ////    try
        ////    {
        ////        if (mBranchBRL.DeleteValid(dBAccess, strRecId, btOldRecStamp))
        ////        {
        ////            mBranchDAL.DeleteMBranch(dBAccess, strRecId);

        ////            dBAccess.CommitTranAndCloseCon();

        ////            return true;
        ////        }
        ////        else
        ////        {
        ////            dBAccess.RollbackTranAndClose();
        ////            if (SysVar.ErrorMessage.Count > 0)
        ////            {
        ////                MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
        ////            }
        ////            return false;
        ////        }
        ////    }
        ////    catch (Exception e)
        ////    {
        ////        dBAccess.RollbackTranAndClose();
        ////        ExceptionManager.ProcessException(e);
        ////        return false;
        ////    }
        ////}
	}
}



